<?php
//TODO: CatalogWizard
class CatalogWizard extends Wizard
{
	var $table = "firm_catalog";
	var $pfx = "firm_";
	var $pid = "catalog_id";
	var $table_adresses;
	var $table_goods;
	var $table_grafik;
	var $table_news;
	var $table_opinions;
	var $table_pages;
	var $table_phones;
	var $table_photoes;
	var $table_prices;
	var $table_tree;
	var $table_videos;
	var $cat;
	var $users;
	var $geo;
	var $catalog_main_cat;
	function __construct(MyDB $db,$tablename,$pfx,$id)
	{
		$this->db = $db;
		if(strlen($tablename) > 0){$this->table = mysql_real_escape_string($tablename);}
		if(strlen($pfx) > 0){$this->pfx = mysql_real_escape_string($pfx);}
		if(strlen($id) > 0){$this->pid = $id;}
		$this->table_adresses = $this->pfx.'adress';
		$this->table_goods = $this->pfx.'goods';
		$this->table_grafik = $this->pfx.'grafik';
		$this->table_news = $this->pfx.'news';
		$this->table_opinions = $this->pfx.'opinions';
		$this->table_pages = $this->pfx.'pages';
		$this->table_phones = $this->pfx.'phones';
		$this->table_photoes = $this->pfx.'photoes';
		$this->table_prices = $this->pfx.'prices';
		$this->table_tree = $this->pfx.'tree';
		$this->table_videos = $this->pfx.'videos';
		$this->cat = new CatalogCategoryWizard();
		$this->catalog_main_cat = $this->cat->byAlias('catalog','catalog');
		$this->users = new UsersWizard;
		$this->geo = new GeoWizard();
		$this->users->init($this->db);
	}
	//TODO: CatalogWizard::getFirmById()
	function getFirmById($id = 0)
	{
		$item = $this->getItem($id);
		if($item != false)
		{
			$item_id = (int)$item['id'];
			//TODO: CatalogWizard::getFirmById()->Item->Adresses
			$item['adresses'] = $this->getFirmAdresses($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Categories
			$item['categories'] = $this->getFirmCategories($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Goods
			$item['goods'] = $this->getFirmGoods($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Grafik
			$item['grafik'] = $this->getFirmGrafik($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->News
			$item['news'] = $this->getFirmNews($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Opinions
			$item['opinions'] = $this->getFirmOpinions($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Pages
			$item['pages'] = $this->getFirmPages($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Phones
			$item['phones'] = $this->getFirmPhones($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Photoes
			$item['photoes'] = $this->getFirmPhotoes($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Prices
			$item['prices'] = $this->getFirmPrices($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->Videos
			$item['videos'] = $this->getFirmVideos($item_id);
			//TODO: CatalogWizard::getFirmById()->Item->link
			$item['link'] = '/catalog/'.$item['alias'].'/';
			//TODO: CatalogWizard::getFirmById()->Item->Logo
			if($item['logo'] == '1')
			{
				$item['logotype']['small'] = UPLOAD.'firm/'.$item['id'].'/logo_small.gif';
				$item['logotype']['med'] = UPLOAD.'firm/'.$item['id'].'/logo_med.gif';
				$item['logotype']['full'] = UPLOAD.'firm/'.$item['id'].'/logo.gif';
			}
			else
			{
				$item['logotype'] = false;
			}
			return $item;
		}
		else return false;
	}
	//TODO: CatalogWizard::getFirmByAlias()
	function getFirmByAlias($alias = '')
	{
		if(strlen($alias) > 0)
		{
			$item = array();
			$item = $this->db->fetch_one("SELECT * FROM $this->table WHERE alias = '".mysql_real_escape_string($alias)."'");
			if($item != false)
			{
				//TODO: CatalogWizard::getFirmByAlias()->Item->Adresses
				$item['adresses'] = $this->getFirmAdresses($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Categories
				$item['categories'] = $this->getFirmCategories($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Goods
				$item['goods'] = $this->getFirmGoods($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Grafik
				$item['grafik'] = $this->getFirmGrafik($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->News
				$item['news'] = $this->getFirmNews($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Opinions
				$item['opinions'] = $this->getFirmOpinions($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Pages
				$item['pages'] = $this->getFirmPages($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Phones
				$item['phones'] = $this->getFirmPhones($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Photoes
				$item['photoes'] = $this->getFirmPhotoes($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Prices
				$item['prices'] = $this->getFirmPrices($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->Videos
				$item['videos'] = $this->getFirmVideos($item['id']);
				//TODO: CatalogWizard::getFirmByAlias()->Item->link
				$item['link'] = '/catalog/'.$item['alias'].'/';
				//TODO: CatalogWizard::getFirmByAlias()->Item->Logo
				if($item['logo'] == '1')
				{
					$item['logotype']['small'] = UPLOAD.'firm/'.$item['id'].'/logo_small.gif';
					$item['logotype']['med'] = UPLOAD.'firm/'.$item['id'].'/logo_med.gif';
					$item['logotype']['full'] = UPLOAD.'firm/'.$item['id'].'/logo.gif';
				}
				else
				{
					$item['logotype'] = false;
				}
				return $item;
			}
			else return false;
		}
		else return false;
	}
	function getFirmAdresses($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->geo->getFullAdressesForFirm($obj_id);
		}
		else return false;
	}
	function getFirmCategories($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			$catalog_categories = $this->db->fetch_list("SELECT category_id FROM $this->table_tree WHERE $this->pid = '".$obj_id."'");
			if(!empty($catalog_categories))	return $this->cat->getCatArray($catalog_categories);
			else return false;
		}
		else return false;
	}
	function getFirmGoods($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_goods WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmGrafik($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_grafik WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmNews($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_news WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmOpinions($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_opinions WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmPages($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_pages WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmPhones($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_phones WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmPhotoes($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_photoes WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmPrices($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_prices WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
	function getFirmVideos($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			return $this->db->fetch_array("SELECT * FROM $this->table_videos WHERE $this->pid = '".$obj_id."'");
		}
		else return false;
	}
}
?>